# 构建阶段
FROM node:20-alpine as build-stage

# 设置工作目录
WORKDIR /app

# 复制package.json和package-lock.json (如果存在)
COPY package*.json ./

# 安装依赖
RUN npm install

# 复制项目文件
COPY . .

# 构建应用
RUN npm run build

# 生产阶段
FROM nginx:stable-alpine as production-stage

# 创建nginx配置文件
RUN mkdir -p /etc/nginx/conf.d
COPY --from=build-stage /app/dist /usr/share/nginx/html

# 创建自定义nginx配置
RUN echo 'server { \
    listen 3000; \
    server_name localhost; \
    location / { \
        root /usr/share/nginx/html; \
        index index.html index.htm; \
        try_files $uri $uri/ /index.html; \
    } \
}' > /etc/nginx/conf.d/default.conf

# 暴露8080端口
EXPOSE 3000

# 启动nginx
CMD ["nginx", "-g", "daemon off;"]